package com.kfm.wiki.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kfm.wiki.entity.Category;
import com.kfm.wiki.entity.Category;
import com.kfm.wiki.service.CategoryService;
import com.kfm.wiki.mapper.CategoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
* @author 10854
* @description 针对表【category(分类)】的数据库操作Service实现
* @createDate 2022-12-15 16:20:09
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Override
    public PageInfo<Category> list(Integer current, Integer pageSize, Category category) {
        PageHelper.startPage(current, pageSize);

        List<Category> list = query(category);

        return new PageInfo<>(list);
    }

    @Override
    public List<Tree<String>> all(Category category) {
        // 查询所有分类
        List<Category> list = query(category);
        // 树形数据
//        return tree(0L, list);
        List<Tree<String>> treeList = TreeUtil.build(list, "0", (treeNode, tree) -> {
            tree.setId(treeNode.getId().toString());
            tree.setParentId(treeNode.getParent().toString());
            tree.setName(treeNode.getName());
            // 扩展属性 ...
            tree.putExtra("parent", treeNode.getParent().toString());
            tree.putExtra("sort", treeNode.getSort());
        });

        return treeList;
    }

    /**
     * 数据库查询
        根据条件查询数据库
     * @param category
     * @return
     */
    private List<Category> query(Category category){
        // 创建搜索条件
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(category.getName())){
            queryWrapper.like("name", category.getName());
        }
        queryWrapper.orderByAsc("sort");
        List<Category> list = super.list(queryWrapper);

        return list;
    }

    /**
     * 树形数据组装
     * @param parentId 父 id
     * @param all  数据
     * @return
     */
    private List<Category> tree(Long parentId, List<Category> all){
        // 子分类 的集合
        List<Category> children = new ArrayList<>();
        // 将集合遍历
        Iterator<Category> iterator = all.iterator();
        while (iterator.hasNext()){
            // 获取当前对象
            Category next = iterator.next();
            // 判断是不是当前分类的子分类
            if (next.getParent().equals(parentId)){
                // 放到 子分类 的集合
                children.add(next);
                // 找 子分类 的 子
                List<Category> childrenList = tree(next.getId(), all);
//                next.setChildren(childrenList);
            }
        }
        return children;
    }

    @Override
    public boolean save(Category category) {
        if (ObjectUtils.isEmpty(category.getId())){
            // 新增分类
            // 设置 id
            category.setId(IdUtil.getSnowflakeNextId());
            // 保存
            return getBaseMapper().insert(category) > 0;
        } else{
            // 编辑信息
            return getBaseMapper().updateById(category) > 0;
        }

    }
}




